package user_api

import (
	"fmt"
	"github.com/gin-gonic/gin"
	"leading/global"
	"leading/models"
	"leading/models/res"
	"leading/utils"
	"net/http"
	"strconv"
	"time"
)

// 登录
func (UserApi) Login(c *gin.Context) {
	var query LoginQuery
	if err := c.ShouldBind(&query); err != nil {
		res.FailWithoutData(err.Error(), c)
		return
	}
	// 判断用户是否存在
	var user models.UserModel
	num := global.DB.Where("username = ?", query.Username).First(&user).Scan(&user).RowsAffected
	if num <= 0 {
		res.FailWithoutData("用户不存在", c)
		return
	}
	// 判断密码
	if !utils.VerifyEncryptPassword(query.Password, user.Password) {
		res.FailWithoutData("密码不正确", c)
		return
	}
	// 保存到redis
	token, _ := utils.GenerateToken(user)
	//result := global.RDB.HSet(global.Ctx, token, user, time.Hour*24)
	result := global.RDB.Set(global.Ctx, "user_"+strconv.Itoa(int(user.ID)), token, time.Hour*24)
	if result.Err() != nil {
		res.FailWithoutData(result.Err().Error(), c)
		return
	}
	var resUser = models.ResUser{
		Nickname:  user.Nickname,
		Username:  user.Username,
		Email:     user.Email,
		Sex:       user.Sex,
		Desc:      user.Desc,
		TotalSize: user.TotalSize,
		UseSize:   user.UseSize,
	}

	var Res models.RegisterRes
	Res.Token = token
	Res.ResUser = resUser
	res.Ok(Res, "success", c)
}

// 注册
func (UserApi) Register(c *gin.Context) {
	//str, _ := utils.GenerateToken("123", "52")
	var user models.UserModel
	if err := c.ShouldBind(&user); err != nil {
		res.FailWithoutData(err.Error(), c)
		return
	}

	if user.Password != user.RePassword {
		res.FailWithoutData("password and repassword no same", c)
		return
	}

	num := global.DB.Where("username = ?", user.Username).First(&user).RowsAffected
	if num > 0 {
		// 存在
		res.FailWithoutData("username already exist", c)
		return
	}
	user.Password, _ = utils.EncryptPassword(user.Password)
	err := global.DB.Create(&user).Error
	if err != nil {
		res.FailWithoutData("create user error", c)
		return
	}
	var resUser = models.ResUser{
		Nickname:  user.Nickname,
		Username:  user.Username,
		Email:     user.Email,
		Sex:       user.Sex,
		Desc:      user.Desc,
		TotalSize: user.TotalSize,
		UseSize:   user.UseSize,
	}
	var res models.RegisterRes
	token, _ := utils.GenerateToken(user)
	res.Token = token
	res.ResUser = resUser
	c.JSON(http.StatusOK, gin.H{"code": 0, "data": res})
}

// 退出登录
func (UserApi) Logout(c *gin.Context) {
	var user models.UserModel
	Res, _ := c.Get("user")
	user = Res.(models.UserModel)
	_, err := global.RDB.Del(global.Ctx, "user_"+strconv.Itoa(int(user.ID))).Result()
	if err != nil {
		res.FailWithoutData(err.Error(), c)
		return
	}
	res.OKWithoutMsg(nil, c)
}

// 测试
func (UserApi) Test(c *gin.Context) {
	//password := "123456"
	//hashPassword, err := utils.EncryptPassword(password)
	//fmt.Println(global.Config)
	//if err != nil {
	//	panic(err)
	//}
	//fmt.Println("hash:", hashPassword)
	//
	//if utils.VerifyEncryptPassword(password, hashPassword) {
	//	fmt.Println("right")
	//} else {
	//	fmt.Println("error")
	//}
	//var test models.TestModel
	//test.Name = "test"
	//err := global.DB.Create(&test)
	//if err != nil {
	//	fmt.Println(err.Error)
	//	return
	//}
	//c.JSON(http.StatusOK, gin.H{"code": 0, "data": test})

	var test models.TestModel
	num := global.DB.Where("name = ?", "test").First(&test).RowsAffected
	if num > 0 {
		fmt.Println(test)
		c.JSON(http.StatusOK, gin.H{"code": 0, "data": test})
		return
	}

	c.String(400, "error")

}
